<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="appnode.Node" %>
<%@ page import="appnode.PMF" %>

<html>
<link rel="stylesheet" href="map.css" type="text/css" media="screen" title="mapcss"></link>
<head>
	<title>Welcome page</title>
</head>
  <body>
	<div id="container">
	<div id="welcome">
	<%
	    UserService userService = UserServiceFactory.getUserService();
	    User user = userService.getCurrentUser();
		String url = "/";
	%>
	<p>Welcome to the AppMapNode application.<p>
	<%
	    if (user != null) {
	%>
		You have signed as <%= user.getNickname() %> <a href="<%= userService.createLogoutURL(url) %>">(sign out)</a><p>
	 
		<%
		String u = user.getNickname();
		PersistenceManager pm = PMF.get().getPersistenceManager();
	    String query = "select from " + Node.class.getName() + " where owner==" + "'"+ u +"'";
	    List<Node> results = (List<Node>) pm.newQuery(query).execute();
		if (results.isEmpty()) {
		%>
		You have not node. <a href="addnewnode.jsp">(add node)</a></p>

		<%
		} else {
			%>
			Select a node :<br>
			<%
			for (Node n : results) {
		%>
		<% String name = n.getNodename();%>
		<a href="map?nodename=<%=name%>"><%= n.getNodename()%></a></p>
		<%
		}
    	}
		pm.close();
		%>
	<%
	    } else {
	%> 
	You are signed out <a href="<%= userService.createLoginURL(url) %>">(sign in)</a>
	<%
	    }
	%>
	</div>
	</div>
	</body>
</html>
